package com.example.cf.grouplist.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cf.grouplist.R;
import com.example.cf.grouplist.view.MyGridView;

public class EatSnake extends AppCompatActivity {

    private String HORIZONTAL = "horizontal", VERTICAL = "vertical";
    private int RIGHT = 1, LEFT = 2, UP = 3, DOWN = 4;
    private MyGridView gridview;
    private MAdapter mAdapter;
    private int[][] data;
    private static final int SIZE = 20;
    private boolean flag = true;
    private int head;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mAdapter.notifyDataSetChanged();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eat_snake);
        gridview = ((MyGridView) findViewById(R.id.gridview));
        initData();
        mAdapter = new MAdapter();
        gridview.setAdapter(mAdapter);
        new Thread() {
            @Override
            public void run() {
                while (flag) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int state = getOrientation();
                    int tail = getTail();
                    add();
                    data[head / SIZE][head % SIZE] = 1;
                    data[tail / SIZE][tail % SIZE] = 0;
                    switch (state) {
                        case 1:
                            head = head + 1;
                            break;
                        case 2:
                            head = head - 1;
                            break;
                        case 3:
                            head = head - 10;
                            break;
                        case 4:
                            head = head + 10;
                            break;
                    }
                    try {
                        data[head / SIZE][head % SIZE] = -1;
                    } catch (ArrayIndexOutOfBoundsException e) {
                        Toast.makeText(EatSnake.this, "死亡", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    handler.sendEmptyMessage(1);
                }
            }
        }.start();
    }

    private void initData() {
        //生成三个连续的数
        data = new int[SIZE][SIZE];
        int beginT = 0;
        while (beginT % SIZE == 0 || beginT % SIZE == SIZE - 1 || beginT / SIZE == 0 || beginT / SIZE == SIZE - 1) {
            beginT = (int) (Math.random() * SIZE * SIZE);
        }
        head = beginT;
        data[beginT / SIZE][beginT % SIZE] = -1;
        int beginX = 0;
        int beginY = 0;
        while ((beginX == 0 && beginY == 0) || (Math.abs(beginX) == 1 && Math.abs(beginY) == 1)) {
            beginX = (int) (Math.random() * 3) - 1;
            beginY = (int) (Math.random() * 3) - 1;
        }
        data[beginT / SIZE + beginY][beginT % SIZE + beginX] = 1;
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.up:
                break;
            case R.id.down:
                break;
            case R.id.right:
                break;
            case R.id.left:
                break;
        }
    }

    public int getOrientation() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == 1) {
                    if (head / SIZE == i) {
                        if (head % SIZE < j)
                            return LEFT;
                        else
                            return RIGHT;
                    } else {
                        if (head/SIZE < i)
                            return UP;
                        else
                            return DOWN;
                    }
                }
            }
        }
        return -33;
    }

    private class MAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return SIZE * SIZE;
        }

        @Override
        public Object getItem(int position) {
            return data[position / 10][position % 10];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = EatSnake.this.getLayoutInflater().inflate(R.layout.gv_item, null);
            TextView tv = (TextView) inflate.findViewById(R.id.tv);
            if (data[position / SIZE][position % SIZE] == 0)
                tv.setBackgroundColor(Color.WHITE);
            else
                tv.setBackgroundColor(Color.RED);
            return inflate;
        }
    }

    public int getTail() {
        int max = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] > data[max / SIZE][max / SIZE]) {
                    max = i * SIZE + j;
                }
            }
        }
        return max;
    }

    public void add() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] > 0) {
                    data[i][j]++;
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag = false;
    }
}
